package com.stone.business.plan.action;
/**
 * -----计划  ACTION---
 */

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.HashMap;
import net.sf.json.JSON;
import net.sf.json.JSONSerializer;
import net.sf.json.JsonConfig;

import com.stone.business.plan.pojo.TPlan;
import com.stone.business.plan.pojo.TPlanBz;
import com.stone.business.plan.service.PlanService;
import com.stone.para.IConstant;
import com.stone.sys.pojo.TBdz;
import com.stone.sys.pojo.TDictionary;
import com.stone.sys.pojo.TMenu;
import com.stone.sys.pojo.TOrgan;
import com.stone.utils.DateFormatTool;
import com.stone.utils.JsonUtils;

import frm.ssh.action.BaseAction;
import frm.ssh.exception.AppException;

public class PlanAction extends BaseAction{
	private PlanService planService;
	private TPlan entity;
	
	private String id;
	private String status;
	private List<TBdz> bdzList;
	private List<TDictionary> dydjList;
	

	private List<TDictionary> sblxList;
	private List<TPlanBz> planbzList;
	
	private String hiddenids;
	private List list;
	private String datafrom;
	private String showtype;
	/**
	 * 进入月度工作计划详细页面
	 * 
	 * @return
	 * @throws AppException
	 */
	public String initMonthPlanDetail() throws AppException {

		// 修改进入
		if (!"0".equals(id.toString())) {

			this.entity = (TPlan) planService.findById(Long.parseLong(id),TPlan.class.getName());

		// 新建进入
		} else {
			
			this.entity = new TPlan();
			this.entity.setStatus("0");
			this.entity.setOuid(super.getSession().getAttribute(IConstant.SESSION_TORGAN_CODE).toString());
		}
		
		setDictionary();
		
		return SUCCESS;
		
	}

	/**
	 * 保存
	 * 
	 * @return
	 * @throws AppException
	 */
	public String saveMonthPlan() throws AppException {
       String sysdate = planService.getSysdate("YYYY-MM-DD");
		Date confDate = DateFormatTool.formatToDate(planService.getSysdate("yyyy-MM-dd HH24:mi"), true);
		if (this.entity.getId() == 0) {
			this.entity.setOuid(super.getSession().getAttribute(IConstant.SESSION_TORGAN_CODE).toString());
		
		
		String strStartDate = super.getRequest().getParameter("entity.startdate");
		// 设置隔日和第一日的查询条件日期
		StringBuffer sql = new StringBuffer();
		sql.append("select to_char(TO_DATE('" + strStartDate + "', 'YYYY-MM-DD'), 'Day') as gr,");
		sql.append(" TO_CHAR((TO_DATE('" + strStartDate + "', 'YYYY-MM-DD') - 2),'YYYY-MM-DD') as zcgr,");
		sql.append(" TO_CHAR((TO_DATE('" + strStartDate + "', 'YYYY-MM-DD') - 4),'YYYY-MM-DD') as tsgr,");
		sql.append(" TO_CHAR((TO_DATE('" + strStartDate + "', 'YYYY-MM-DD') - 1),'YYYY-MM-DD') as zcdyr,");
		sql.append(" TO_CHAR((TO_DATE('" + strStartDate + "', 'YYYY-MM-DD') - 3),'YYYY-MM-DD') as tsdyr");
		sql.append(" from dual");
		
		List<HashMap<String, Object>> gr = planService.findListBySql(sql.toString());
		
		String xqj = gr.get(0).get("gr").toString() ;
		String zcgr = gr.get(0).get("zcgr").toString().compareTo(sysdate) <=0 ? sysdate :gr.get(0).get("zcgr").toString();
		String tsgr = gr.get(0).get("tsgr").toString().compareTo(sysdate) <=0 ? sysdate :gr.get(0).get("tsgr").toString();
		String zcdyr = gr.get(0).get("zcdyr").toString().compareTo(sysdate) <=0 ? sysdate :gr.get(0).get("zcdyr").toString();
		String tsdyr = gr.get(0).get("tsdyr").toString().compareTo(sysdate) <=0 ? sysdate :gr.get(0).get("tsdyr").toString();		
		
		// 开始日期是周一隔日和第二日再往前推两天
		if ("星期一".equals(xqj)) {
			
			entity.setGr(DateFormatTool.formatToDate(tsgr, false));
			entity.setDyr(DateFormatTool.formatToDate(tsdyr, false));
			
			// 开始日期是周二隔日再往前推两天
		} else if ("星期二".equals(xqj)) {
			entity.setGr(DateFormatTool.formatToDate(tsgr, false));
		} else {
			entity.setGr(DateFormatTool.formatToDate(zcgr, false));
			entity.setDyr(DateFormatTool.formatToDate(zcdyr, false));
		}
		}
	String flag=super.getRequest().getParameter("flag");
	if(flag!=null){
		entity.setStatus(flag);
	}
		String rtn = planService.doSave(this.entity, this.planbzList, this.hiddenids);
		
		setDictionary();
		
		super.setInfo(rtn);

		return SUCCESS;

	}
	
	/**
	 * 删除
	 * 
	 * @return
	 * @throws AppException
	 */
	public String deleteMonthPlan() throws AppException {
		
		planService.doDelAll(id, TPlan.class.getName());
		planService.deleteByDHql("planid in (" + id + ")", null, TPlanBz.class.getName());
		
		return "DELETE_SUCCESS";
	}

	/**
	 * 提交 直接审核
	 * 
	 * @return
	 * @throws AppException
	 */
	public String confirmMonthPlan() throws AppException {
		planService.updateByDHql("status = '" + super.getRequest().getParameter("flag") + "'", "id in (" + this.id + ")", null, null, TPlan.class.getName());
		return "DELETE_SUCCESS";	
	}
	
	/**
	 * 获得字典项目
	 */
	private void setDictionary() {
		
		// 变电站
		this.bdzList = planService.findListByHql(" from TBdz order by sortid");
		/*-----------add by fanghf 2012.3.15变电站下拉列表显示由电压等级和变电站名称拼接起来begin-----------*/
		if(this.bdzList != null&&this.bdzList.size() > 0){
			for(int i=0;i<this.bdzList.size();i++){
				TBdz tbdz = this.bdzList.get(i);
				if(tbdz != null){
					tbdz.setBdzname(tbdz.getDydj() + "Kv" + tbdz.getBdzname());
				}
			}
		}
		/*-----------add by fanghf 2012.3.15变电站下拉列表显示由电压等级和变电站名称拼接起来begin-----------*/
		// 电压等级
		this.dydjList = planService.findListByHql(" from TDictionary where itemType = 'DYDJ' order by sortid");
		// 电压等级
		this.sblxList = planService.findListByHql(" from TDictionary where itemType = 'SBLX' order by sortid");
		
		TOrgan o  = (TOrgan)super.getSession().getAttribute(IConstant.SESSION_TORGAN);
		String where = (o.getOrganCode().toString().length() <= 6 || "1".equals(super.getSession().getAttribute("SESSION_ISADMIN"))) ? "" : " and organCode = '" + o.getOrganCode().toString() + "'";
		
		// 班组
		List<TOrgan> list = planService.findListByHql(" from TOrgan where organNameEn like '%B' " + where  + " order by id");
		
		if ("0".equals(id)) {
			
			this.planbzList = new ArrayList<TPlanBz>();
			int i = 0;
			for (TOrgan organ : list) {
				TPlanBz bz = new TPlanBz();
				bz.setId(Long.parseLong("0"));
				bz.setDelflag("0");
				bz.setBzid(organ.getId());
				bz.setBzname(organ.getOrganName());
				this.planbzList.add(bz);
				i++;
			}
		} else {
			where = (o.getOrganCode().toString().length() <= 6 || "1".equals(super.getSession().getAttribute("SESSION_ISADMIN"))) ? "" : " and bzid  = '" + o.getId().toString() + "'";
			this.planbzList = planService.findListByHql(" from TPlanBz where planid = '" + entity.getId() + "'" +  where  + " order by delflag");
		}
		
	}
	
	/**
	 * 进入详细页面
	 * 
	 * @return
	 * @throws AppException
	 */
	public String initDetail() throws AppException {
	       
		String   selectpage=super.getSearchBean().getCon1();

		// 修改进入
		if (!"0".equals(id)) {

			this.entity = (TPlan) planService.findById(Long.parseLong(id),TPlan.class.getName());
			return SUCCESS;

		// 新建进入
		} else {
			
			this.entity = new TPlan();
			return SUCCESS;

		}
		
	}

	/**
	 * 保存、审核
	 * 
	 * @return
	 * @throws AppException
	 */
	public String save() throws AppException {
		
		String   selectpage=super.getSearchBean().getCon1();
		
		Date confDate = DateFormatTool.formatToDate(planService.getSysdate("yyyy-MM-dd HH24:mi"), true);
		if (this.entity.getId() == 0) {
			
			this.entity.setOuid(super.getSession().getAttribute(IConstant.SESSION_TORGAN_CODE).toString());
		}
		String rtn = planService.doSave(this.entity, this.planbzList,this.hiddenids);
		
		super.setInfo(rtn);

		return SUCCESS;

	}
	
	/**
	 * -----提交计划----
	 * 
	 * @return
	 * @throws AppException
	 */
	public String submit() throws AppException {
		
		Date confDate = DateFormatTool.formatToDate(planService.getSysdate("yyyy-MM-dd HH24:mi"), true);
		this.entity = (TPlan) planService.findById(Long.parseLong(id),TPlan.class.getName());
		
		String rtn = planService.doSubmit(this.entity,status);
		super.setInfo(rtn);
		if (status == "1"){
			return "SUBMIT_SUCCESS";
			
		}else {
			
			return "PASS_SUCCESS";
		}
	}
	/**
	 * 班组
	 * @return
	 * @throws AppException
	 */	
	public void getBzRw()  throws IOException, AppException {
		
		//TPlanBz planBz = (TPlanBz) planService.findById(Long.parseLong(id), TPlanBz.class.getName());
		// 计划班组
		this.planbzList = planService.findListByHql(" from TPlanBz where delflag='0' and planid = '" + id + "' order by delflag");
		
        JsonConfig jsonConfig = JsonUtils.configJson(new String[] {}, "yyyy-MM-dd");
    	
        JSON json = JSONSerializer.toJSON(planbzList, jsonConfig);

        super.getResponse().getWriter().print(json.toString());
	}

	//进入工作准备详细add by fanghf 2012.3.17
	public String initTabDetail(){
		return "TAB_SUCCESS";
	}
	
	//
	public String getGzapList(){
		String sql = "";
		String conditions = " where status = '9'";
		//月度计划
		if("1".equals(datafrom)){
			sql  = 	"select a.id,a.teamnames,a.startdate,a.enddate,a.powerrange,a.voltagelevel,a.workcontent,a.status," +
	   				" case when b.id is null then '0' else '1' end as ishavegzd" +
	   				" from tab_plan a left join tab_gzrwd b" +
	   				" on b.planid = a.id";
		//临时安排
		}else if("2".equals(datafrom)||"3".equals(datafrom)){
			sql  =	"select a.id,a.bzname as teamnames,a.startdate,a.enddate,a.tdfw as powerrange,a.dydj as voltagelevel,a.gzxm as workcontent,a.status," + 
					" case when b.id is null then '0' else '1' end as ishavegzd" + 
					" from tab_lsap a left join tab_gzrwd b" + 
					" on b.planid = a.id";
		}

		
		if ("3".equals(super.getSearchBean().getCon20())) {
			conditions += " and a.startdate - trunc(sysdate) = 2";
		}else if("4".equals(super.getSearchBean().getCon20())){
			conditions += " and a.startdate - trunc(sysdate) = 1";
		}else if("5".equals(super.getSearchBean().getCon20())){
			conditions += " and a.startdate - trunc(sysdate) = 0";
		}
	
		TOrgan o  = (TOrgan)super.getSession().getAttribute(IConstant.SESSION_TORGAN);
		if (o.getOrganCode().toString().length() > 6 && !"1".equals(super.getSession().getAttribute("SESSION_ISADMIN"))) {
			conditions += " and a.ouid = '" + o.getOrganCode() + "'";
		}
		//临时生产工作安排
		if("2".equals(datafrom)){
			conditions += " and a.state = '0'";
		//临时管理工作安排
		}else if("3".equals(datafrom)){
			conditions += " and a.state = '1'";
		}
		
		if("1".equals(showtype)||"4".equals(showtype)||"7".equals(showtype)){
			conditions += " and b.id is not null";
		}else if("2".equals(showtype)||"5".equals(showtype)||"8".equals(showtype)){
			conditions += " and b.id is null";
		}
		
		sql += conditions;
		list = planService.findListBySql(sql);
		
		return "GET_LIST_SUCCESS";
	}
	public List<TBdz> getBdzList() {
		return bdzList;
	}

	public List<TDictionary> getDydjList() {
		return dydjList;
	}

	public List<TDictionary> getSblxList() {
		return sblxList;
	}

	public void setPlanService(PlanService planService) {
		this.planService = planService;
	}

	public TPlan getEntity() {
		return entity;
	}

	public void setEntity(TPlan entity) {
		this.entity = entity;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public List<TPlanBz> getPlanbzList() {
		return planbzList;
	}

	public void setPlanbzList(List<TPlanBz> planbzList) {
		this.planbzList = planbzList;
	}

	public String getHiddenids() {
		return hiddenids;
	}

	public void setHiddenids(String hiddenids) {
		this.hiddenids = hiddenids;
	}
	
	public List getList() {
		return list;
	}

	public void setList(List list) {
		this.list = list;
	}

	public String getDatafrom() {
		return datafrom;
	}

	public void setDatafrom(String datafrom) {
		this.datafrom = datafrom;
	}

	public String getShowtype() {
		return showtype;
	}

	public void setShowtype(String showtype) {
		this.showtype = showtype;
	}

	public PlanService getPlanService() {
		return planService;
	}

	public void setBdzList(List<TBdz> bdzList) {
		this.bdzList = bdzList;
	}

	public void setDydjList(List<TDictionary> dydjList) {
		this.dydjList = dydjList;
	}

	public void setSblxList(List<TDictionary> sblxList) {
		this.sblxList = sblxList;
	}


}
